package distributeTables

import (
	"context"
	"devops-super/internal/consts/cacheConsts"
	"devops-super/internal/dao"
	"devops-super/internal/model/mymodel"
	"devops-super/internal/service"
)

// GetOneByDbConnAndTable 获取单条分发表- 根据dbconn和表名
func (*sDistributeTables) GetOneByDbConnAndTable(ctx context.Context, dbConn, table string) (out *mymodel.DistributeTableResponse, err error) {
	cacheKey := cacheConsts.GetDistributeTablesTimeGroupKey(dbConn, table)
	// 先从缓存获取
	if v, _ := service.GetCacheInstance().Get(ctx, cacheKey); !v.IsEmpty() {
		out = &mymodel.DistributeTableResponse{}
		if err = v.Struct(out); err == nil {
			return out, nil
		}
	}
	_ = dao.AppDistributeTables.Ctx(ctx).
		Where(dao.AppDistributeTables.Columns().ServerKey, dbConn).
		Where(dao.AppDistributeTables.Columns().Table, table).Scan(&out)
	// 设置缓存
	if out != nil {
		_ = service.GetCacheInstance().Set(ctx, cacheKey, out, 0)
	}
	return out, nil
}
